{% set pypi_name = 'monasca-ui' %}
{% set source = fetch_source('https://tarballs.openstack.org/monasca-ui/monasca-ui-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}

%if 0%{?rhel} || 0%{?fedora}
%global rdo 1
%global http_dashboard_dir %{_datarootdir}/openstack-dashboard
%else
%global http_dashboard_dir /srv/www/openstack-dashboard
%endif
Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        OpenStack Monasca Horizon plugin
License:        {{ license('Apache-2.0') }}
Group:          Development/Languages/Python
URL:            https://wiki.openstack.org/wiki/Monasca
Source0:        {{ source|basename }}
BuildRequires:  fdupes
BuildRequires:  {{ py2pkg('horizon') }}
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('python-monascaclient') }}
BuildRequires:  {{ py3('pbr') }}
Requires:       python3-{{ pypi_name }} = %{version}-%{release}
BuildArch:      noarch

%description
The Monasca UI Horizon plugin adds Monitoring panel to the OpenStack dashboard.

%package -n python3-{{ pypi_name }}
Summary:       Monasca Plugin for Horizon - Python module
Requires:      {{ py3('Django') }}
Requires:      {{ py3('python-monascaclient') }}
Requires:      {{ py3('pbr') }}

%description -n python3-{{ pypi_name }}
The Monasca UI Horizon plugin adds Monitoring panel to the OpenStack dashboard.

%if 0%{?suse_version}
%package -n grafana-monasca-ui-drilldown
Summary:       Scripted Grafana dashboard for alarm drilldown links
BuildRequires: grafana
Requires:      grafana

%description -n grafana-monasca-ui-drilldown
This adds a scripted dashboard to Grafana 2 and higher that the Monitoring
panel's Alarms tab links to when generating drilldown links for alarms.
%endif

%prep
%autosetup -p1 -n {{ pypi_name }}-{{ upstream_version }}
%py_req_cleanup

%build
%py3_build

%install
%py3_install

install -m 0755 -d %{buildroot}%{http_dashboard_dir}/openstack_dashboard/enabled/
install -m 0755 -d %{buildroot}%{http_dashboard_dir}/openstack_dashboard/conf/
cp -a monitoring/enabled/_50_admin_add_monitoring_panel.py* %{buildroot}%{http_dashboard_dir}/openstack_dashboard/enabled/
cp -a monitoring/conf/monitoring_policy.json %{buildroot}%{http_dashboard_dir}/openstack_dashboard/conf/
%if 0%{?suse_version}
install -m 0755 -d %{buildroot}%{_datadir}/grafana/public/dashboards/
install -m 644 grafana-dashboards/drilldown.js %{buildroot}%{_datadir}/grafana/public/dashboards/
%endif
%fdupes %{buildroot}%{python3_sitelib}
%fdupes %{buildroot}%{http_dashboard_dir}

%post
su %{apache_user} -s /bin/sh -c "python3 %{http_dashboard_dir}/manage.py collectstatic --noinput --clear > /dev/null"

%postun
su %{apache_user} -s /bin/sh -c "python3 %{http_dashboard_dir}/manage.py collectstatic --noinput --clear > /dev/null"

%files
%{http_dashboard_dir}/openstack_dashboard/enabled/_50_admin_add_monitoring_panel.py*
%{http_dashboard_dir}/openstack_dashboard/conf/monitoring_policy.json
%license LICENSE
%doc ChangeLog README.rst

%if 0%{?suse_version}
%files -n grafana-monasca-ui-drilldown
%license LICENSE
%{_datadir}/grafana/public/dashboards/drilldown.js
%endif

%files -n python3-{{ pypi_name }}
%license LICENSE
%doc README.rst
%{python3_sitelib}/monitoring
%{python3_sitelib}/monasca_ui-*.egg-info

%changelog
